package com.mframe.persist;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.mframe.exception.MarsRuntimeException;
import com.mframe.query.PageData;
import com.mframe.query.SpringBeanRowMapper;
import com.mframe.query.SqlBuilder;

public class OracleDAOImpl extends PublicDAOImpl{
	private JdbcTemplate jdbcTemplate;
	
	public enum MatchType {
		EQ, LIKE, LT, GT, LE, GE;
	}
	public List<Map<String, Object>> callForList(String storeName,
			Object[] paramValues) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object callForObj(String storeName, Object[] paramValues, int sqlType) {
		// TODO Auto-generated method stub
		return null;
	}

	public void callForObj(String storeName, Object[] paramValues) {
		// TODO Auto-generated method stub
		
	}

	public Long getNextSequence(String seqName) {
	    	Long lSql =Long.valueOf(0);
	    	String seqsql="select "+seqName+".nextval from dual";
	    	lSql = jdbcTemplate.queryForLong(seqsql);
		return lSql;
	}

	public void executeSql(String sql) {
		jdbcTemplate.execute(sql);
	}

	public <T> T queryForBean(Class<T> returnClazz, String sql,
			Object[] paramsValues) {
    	    	RowMapper<T> rm = SpringBeanRowMapper.newInstance(returnClazz);
    	    	List<T> result =jdbcTemplate.query(sql, rm,paramsValues);
    	    	if(result==null || result.size()==0)
    	    	    return null;
    	    	if(result.size()>1){
    	    	    throw new MarsRuntimeException("数据不唯一，返回多条或0条数据！");
    	    	}
    	    	return result.get(0);
	}

	public <T> List<T> queryForBeanList(Class<T> clazz, String sql,
			Object[] args) {
	    	RowMapper<T> rm = SpringBeanRowMapper.newInstance(clazz);
	    	List<T> result =jdbcTemplate.query(sql, rm,args);
		return result==null?new ArrayList<T>(0):result;
	}

	public Integer queryForInt(String sql, Object[] paramsValues) {
		return jdbcTemplate.queryForInt(sql,paramsValues);
	}

	public <T> List<T> queryForList(String sql, Object[] args,
			Class<T> elementType) {
		return jdbcTemplate.queryForList(sql,args,elementType);
	}

	public Long queryForLong(String sql, Object[] paramsValues) {
	    return jdbcTemplate.queryForLong(sql,paramsValues);
	}

	public Map<String, Object> queryForMap(String sql, Object[] args) {
		return jdbcTemplate.queryForMap(sql, args);
	}

	public Object queryForObject(String sql, Object[] paramsValues) {
	    	Map<String,Object> map=this.queryForMap(sql, paramsValues);
	    	if(map!=null&&!map.values().isEmpty()){
			return map.values().iterator().next();
		}
		return null;
	}

	public <T> PageData queryForPageData(String sql, Class<T> clazz,
			int topage, int pagesize, Object[] paramsValues) {
	    	RowMapper<T> rm = SpringBeanRowMapper.newInstance(clazz);
	    	// TODO Auto-generated method stub
		return null;
	}

	public Object queryUnique(SqlBuilder builder) {
		// TODO Auto-generated method stub
		return null;
	}	

	public Object queryBySqlBuilder(SqlBuilder builder) {
		// TODO Auto-generated method stub
		return null;
	}

	////////////注入///////////////////
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}	

}
